package iamutkarshtiwari.github.io.ananas.editimage.fragment;

import iamutkarshtiwari.github.io.ananas.ResourceTable;
import iamutkarshtiwari.github.io.ananas.editimage.EditImageAbility;
import ohos.agp.components.*;
import ohos.agp.render.*;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;

public class BrightnessFragment extends ComponentContainer {
    private Context mContext;
    PageSlider pageSlider;
    Image back;
    public Slider slider;
    private EditImageAbility abilitySlice;

    public BrightnessFragment(Context context, PageSlider pageSlider) {
        super(context);
        this.mContext = context;
        this.abilitySlice = (EditImageAbility) context;
        this.pageSlider = pageSlider;
        initView();
    }

    public BrightnessFragment(Context context, AttrSet attrSet) {
        super(context, attrSet);
    }

    public BrightnessFragment(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    /**
     * 切换到Brightness界面，显示需要的控件
     */
    public void onShow() {

        abilitySlice.mode = EditImageAbility.MODE_BRIGHTNESS;
        abilitySlice.setMainImageVisibility(Component.HIDE);
        abilitySlice.brightnessView.setPixelMap(abilitySlice.getMainBit());
        abilitySlice.brightnessView.setVisibility(VISIBLE);
//        textStickerView.setVisibility(Component.VISIBLE);
    }

    /**
     * brightness主界面的布局初始化
     */
    private void initView() {
        Component component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_fragment_brightness_layout, null, false);
        addComponent(component);

        back = (Image) component.findComponentById(ResourceTable.Id_back_to_main);
        slider = (Slider) component.findComponentById(ResourceTable.Id_seekbar);

        back.setClickedListener(component1 -> backToMain());

        slider.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean b) {
                float value = i - (slider.getMax() / 2);
                abilitySlice.getUITaskDispatcher().asyncDispatch(() -> {
                    //更新UI的操作
                    abilitySlice.brightnessView.setPixelMap(handleImageEffect(abilitySlice.getMainBit(), value / 10f));
                });

            }

            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {

            }
        });
    }

    /**
     * 根据亮度设置生成一个新的图像
     *
     * @param bitmap 原图
     * @param lum    进行缩放后的亮度值
     * @return 修改后的新bitmap
     */
    public static PixelMap handleImageEffect(PixelMap bitmap, float lum) {
        //传进来的bitmap默认不能修改  所以再创建一个bm
        PixelMap.InitializationOptions opt = new PixelMap.InitializationOptions();
        opt.pixelFormat = PixelFormat.ARGB_8888;
        PixelMap bm = PixelMap.create(bitmap, opt);
        //画布
        Canvas canvas = new Canvas(new Texture(bm));
        //抗锯齿
        Paint paint = new Paint();
        //修改亮度
        ColorMatrix lumMatrix = new ColorMatrix();
        //r g b a    1 表示全不透明
        lumMatrix.setScale(lum, lum, lum, 1);
        lumMatrix.setMatrix(new float[]{1, 0, 0, 0, lum, 0, 1, 0, 0, lum, 0, 0, 1, 0, lum, 0, 0, 0, 1, 0});
        ColorMatrix imageMatrix = new ColorMatrix();
        imageMatrix.postConcat(lumMatrix);
        //为画笔设置颜色过滤器
        paint.setColorMatrix(imageMatrix);
        canvas.drawPixelMapHolder(new PixelMapHolder(bitmap), 0, 0, paint);
        return bm;
    }

    /**
     * 返回主界面
     */
    public void backToMain() {
        abilitySlice.mode = EditImageAbility.MODE_NONE;
        pageSlider.setCurrentPage(0, false);
        pageSlider.setVisibility(Component.HIDE);
        abilitySlice.brightnessView.setVisibility(Component.HIDE);
        abilitySlice.mainImage.setVisibility(VISIBLE);
        abilitySlice.saveOrApply(false);
        slider.setProgressValue(1000);
    }

    /**
     * Brightness修改后的效果应用
     */
    public void applyBrightness() {
        abilitySlice.changeMainBitmap(abilitySlice.brightnessView.getPixelMap(), true);
        abilitySlice.brightnessView.release();
        backToMain();
    }
}
